#include "stdafx.h"

#include "CoreUtil.h"
#include "SummaryProperties.h"

using namespace System::Runtime::InteropServices;

using namespace DsoFileNET;

SummaryProperties::SummaryProperties(ptr<IPropertySetStorage> %propStorage, bool readOnly, DsoFileNET::FileOpenOptions options)
{
	IPropertySetStorage *tmp = propStorage.GetInterface();
	this->storage.Attach(tmp);
	tmp->Release();

	this->summProps = gcnew Dictionary<DsoFileNET::SummaryInformationProperty, CustomProperty<DsoFileNET::SummaryInformationProperty>^>();
	this->docProps = gcnew Dictionary<DsoFileNET::UserDefinedProperty, CustomProperty<DsoFileNET::UserDefinedProperty>^>();

	ptr<IPropertyStorage> propStore;

	// Summary Properties
	CoreUtil::OpenPropertyStorage(this->storage, FMTID_SummaryInformation, readOnly, options, propStore);
	if (propStore)
	{
		CoreUtil::LoadPropertySetList<SummaryInformationProperty>(propStore, this->codePageSI, this->summProps);
		propStore.Release();
	}

	// Custom Properties
	CoreUtil::OpenPropertyStorage(this->storage, FMTID_SummaryInformation, readOnly, options, propStore);
	if (propStore)
	{
		CoreUtil::LoadPropertySetList<UserDefinedProperty>(propStore, this->codePageUD, this->docProps);
		propStore.Release();
	}
}

SummaryProperties::~SummaryProperties()
{
	if (this->storage)
		this->storage.Release();
}

String^ SummaryProperties::Title::get()
{
	String^ ret = String::Empty;
	ReadProperty<SummaryInformationProperty, String^>(this->summProps, SummaryInformationProperty::Title, ret);
	return ret;
}

void SummaryProperties::Title::set(String^ value)
{
	throw gcnew NotImplementedException();
}

String^ SummaryProperties::Subject::get()
{
	String^ ret = String::Empty;
	ReadProperty<SummaryInformationProperty, String^>(this->summProps, SummaryInformationProperty::Subject, ret);
	return ret;
}

void SummaryProperties::Subject::set(String^ value)
{
	throw gcnew NotImplementedException();
}

String^ SummaryProperties::Author::get()
{
	String^ ret = String::Empty;
	ReadProperty<SummaryInformationProperty, String^>(this->summProps, SummaryInformationProperty::Author, ret);
	return ret;
}

void SummaryProperties::Author::set(String^ value)
{
	throw gcnew NotImplementedException();
}

String^ SummaryProperties::Keywords::get()
{
	String^ ret = String::Empty;
	ReadProperty<SummaryInformationProperty, String^>(this->summProps, SummaryInformationProperty::Keywords, ret);
	return ret;
}

void SummaryProperties::Keywords::set(String^ value)
{
	throw gcnew NotImplementedException();
}

String^ SummaryProperties::Comments::get()
{
	String^ ret = String::Empty;
	ReadProperty<SummaryInformationProperty, String^>(this->summProps, SummaryInformationProperty::Comments, ret);
	return ret;
}

void SummaryProperties::Comments::set(String^ value)
{
	throw gcnew NotImplementedException();
}


String^ SummaryProperties::Template::get()
{
	String^ ret = String::Empty;
	ReadProperty<SummaryInformationProperty, String^>(this->summProps, SummaryInformationProperty::Template, ret);
	return ret;
}

String^ SummaryProperties::LastSavedBy::get()
{
	String^ ret = String::Empty;
	ReadProperty<SummaryInformationProperty, String^>(this->summProps, SummaryInformationProperty::LastAuthor, ret);
	return ret;
}

void SummaryProperties::LastSavedBy::set(String^ value)
{
	throw gcnew NotImplementedException();
}

String^ SummaryProperties::RevisionNumber::get()
{
	String^ ret = String::Empty;
	ReadProperty<SummaryInformationProperty, String^>(this->summProps, SummaryInformationProperty::RevNumber, ret);
	return ret;
}

int SummaryProperties::TotalEditTime::get()
{
	int ret = 0;
	ReadProperty<SummaryInformationProperty, int>(this->summProps, SummaryInformationProperty::EditTime, ret);
	return ret;
}

System::DateTime SummaryProperties::DateLastPrinted::get()
{
//	throw gcnew NotImplementedException();
	DateTime ret = DateTime::MinValue;
	ReadProperty<SummaryInformationProperty, DateTime>(this->summProps, SummaryInformationProperty::LastPrinted, ret);
	return ret;
}

System::DateTime SummaryProperties::DateCreated::get()
{
//	throw gcnew NotImplementedException();
	DateTime ret = DateTime::MinValue;
	ReadProperty<SummaryInformationProperty, DateTime>(this->summProps, SummaryInformationProperty::CreateDtm, ret);
	return ret;
}

System::DateTime SummaryProperties::DateLastSaved::get()
{
	DateTime ret = DateTime::MinValue;
	ReadProperty<SummaryInformationProperty, DateTime>(this->summProps, SummaryInformationProperty::LastSaveDtm, ret);
	return ret;
//	throw gcnew NotImplementedException(); get_DateLastSaved
}

int SummaryProperties::PageCount::get()
{
	int ret = 0;
	ReadProperty<SummaryInformationProperty, int>(this->summProps, SummaryInformationProperty::PageCount, ret);
	return ret;
}

int SummaryProperties::WordCount::get()
{
	int ret = 0;
	ReadProperty<SummaryInformationProperty, int>(this->summProps, SummaryInformationProperty::WordCount, ret);
	return ret;
}

int SummaryProperties::CharacterCount::get()
{
	int ret = 0;
	ReadProperty<SummaryInformationProperty, int>(this->summProps, SummaryInformationProperty::CharCount, ret);
	return ret;
}

Object^ SummaryProperties::Thumbnail::get()
{
	throw gcnew NotImplementedException();
}

String^ SummaryProperties::ApplicationName::get()
{
	String^ ret = String::Empty;
	ReadProperty<SummaryInformationProperty, String^>(this->summProps, SummaryInformationProperty::ApplicationName, ret);
	return ret;
}

int SummaryProperties::DocumentSecurity::get()
{
	int ret = 0;
	ReadProperty<SummaryInformationProperty, int>(this->summProps, SummaryInformationProperty::DocumentSecurity, ret);
	return ret;
}

String^ SummaryProperties::Category::get()
{
	String^ ret = String::Empty;
	ReadProperty<UserDefinedProperty, String^>(this->docProps, UserDefinedProperty::Category, ret);
	return ret;
}

void SummaryProperties::Category::set(String^ value)
{
	throw gcnew NotImplementedException();
}

String^ SummaryProperties::PresentationFormat::get()
{
	String^ ret = String::Empty;
	ReadProperty<UserDefinedProperty, String^>(this->docProps, UserDefinedProperty::PresentationFormat, ret);
	return ret;
}

int SummaryProperties::ByteCount::get()
{
	int ret = 0;
	ReadProperty<UserDefinedProperty, int>(this->docProps, UserDefinedProperty::ByteCount, ret);
	return ret;
}

int SummaryProperties::LineCount::get()
{
	int ret = 0;
	ReadProperty<UserDefinedProperty, int>(this->docProps, UserDefinedProperty::LineCount, ret);
	return ret;
}

int SummaryProperties::ParagraphCount::get()
{
	int ret = 0;
	ReadProperty<UserDefinedProperty, int>(this->docProps, UserDefinedProperty::ParagraphCount, ret);
	return ret;
}

int SummaryProperties::SlideCount::get()
{
	int ret = 0;
	ReadProperty<UserDefinedProperty, int>(this->docProps, UserDefinedProperty::SlideCount, ret);
	return ret;
}

int SummaryProperties::NoteCount::get()
{
	int ret = 0;
	ReadProperty<UserDefinedProperty, int>(this->docProps, UserDefinedProperty::NoteCount, ret);
	return ret;
}

int SummaryProperties::HiddenSlideCount::get()
{
	int ret = 0;
	ReadProperty<UserDefinedProperty, int>(this->docProps, UserDefinedProperty::HiddenCount, ret);
	return ret;
}

int SummaryProperties::MultimediaClipCount::get()
{
	int ret = 0;
	ReadProperty<UserDefinedProperty, int>(this->docProps, UserDefinedProperty::MultimediaClipCount, ret);
	return ret;
}


String^ SummaryProperties::Manager::get()
{
	String^ ret = String::Empty;
	ReadProperty<UserDefinedProperty, String^>(this->docProps, UserDefinedProperty::Manager, ret);
	return ret;
}

void SummaryProperties::Manager::set(String^ value)
{
	throw gcnew NotImplementedException();
}
String^ SummaryProperties::Company::get()
{
	String^ ret = String::Empty;
	ReadProperty<UserDefinedProperty, String^>(this->docProps, UserDefinedProperty::Company, ret);
	return ret;
}
void SummaryProperties::Company::set(String^ value)
{
	throw gcnew NotImplementedException();
}

int SummaryProperties::CharacterCountWithSpaces::get()
{
	int ret = 0;
	ReadProperty<UserDefinedProperty, int>(this->docProps, UserDefinedProperty::CharacterCountWithSpaces, ret);
	ReadProperty<UserDefinedProperty, int>(this->docProps, UserDefinedProperty::CharacterCountWithSpaces, ret);
	return ret;
}

bool SummaryProperties::SharedDocument::get()
{
	bool ret = false;
	ReadProperty<UserDefinedProperty, bool>(this->docProps, UserDefinedProperty::SharedDocument, ret);
	return ret;
}

String^ SummaryProperties::Version::get()
{
	String^ ret = String::Empty;
	ReadProperty<UserDefinedProperty, String^>(this->docProps, UserDefinedProperty::Version, ret);
	return ret;
}

Object^ SummaryProperties::DigitalSignature::get()
{
	throw gcnew NotImplementedException();
}

generic <typename Tprop, typename Tout>
bool SummaryProperties::ReadProperty(Dictionary<Tprop, CustomProperty<Tprop>^>^ props, Tprop propId, Tout %result)
{
	CustomProperty<Tprop>^ tmp = nullptr;

	if (props->TryGetValue(propId, tmp))
	{
		result = safe_cast<Tout>(tmp->Value);
		return true;
	}
	return false;
}
